package com.mytest.controller;

import com.mytest.Movie;
import com.mytest.entity.Student;
import com.mytest.service.MovieService;
import com.mytest.util.LogLocalityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
public class MyController {

    Logger logger = LoggerFactory.getLogger(getClass());
    public static Map<String,Thread> map = new HashMap();

    @Autowired
    Movie movie;
    @Autowired
    MovieService movieService;
    @Autowired
    Student student;

    @GetMapping("/test")
    public String test(){
        System.out.println(student.getScore());
        return "ok";
    }


    @GetMapping("/getFilm1")
    public Movie getFilm1() {

        return movie;
    }

    @GetMapping("/service1")
    public String service1() {

        Thread thread = Thread.currentThread();
        System.out.println(thread);

        movieService.method();
        return "service1";
    }

    @GetMapping("/service2")
    public String service2() {

        Thread thread = Thread.currentThread();
        System.out.println(thread);

        movieService.method();
        return "service2";
    }

    @GetMapping("/service3")
    public String service3(@RequestParam(value="param") String param) {

        SimpleDateFormat date = new SimpleDateFormat("yyyy-mm-dd  HH:mm:ss");
        Thread thread = Thread.currentThread();
        System.out.println(date.format(new Date()) + " "+thread+" param="+param);

        movieService.method1(param);

        return "service3";
    }

    @GetMapping("/service4")
    public String service4(@RequestParam(value="param") String param) {

        SimpleDateFormat date = new SimpleDateFormat("yyyy-mm-dd  HH:mm:ss");
        Thread thread = Thread.currentThread();
        System.out.println(date.format(new Date()) + " "+thread+" param="+param);

        movieService.method2(param);

        return "service4";
    }

    //5 6 interrupt test
    @GetMapping("/service5")
    public String service5() {

        SimpleDateFormat date = new SimpleDateFormat("yyyy-mm-dd  HH:mm:ss");
        Thread thread = Thread.currentThread();
        map.put("t1",thread);
        logger.info(LogLocalityUtil.getLogLocality() + " ThreadId:"+thread.getId());
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            logger.error("error",e);
            logger.info(LogLocalityUtil.getLogLocality()+"interrupt by other thread once");
        }
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            logger.error("error",e);
            logger.info(LogLocalityUtil.getLogLocality()+"interrupt by other thread twice");
        }



        return "service5";
    }

    @GetMapping("/service6")
    public String service6() {

        SimpleDateFormat date = new SimpleDateFormat("yyyy-mm-dd  HH:mm:ss");
        Thread thread = Thread.currentThread();
        logger.info(LogLocalityUtil.getLogLocality() + " ThreadId:"+thread.getId());
        Thread thread1 = map.get("t1");
        //Thread thread1 = map.get("t2");
        thread1.interrupt();

        return "service6";
    }

    @GetMapping("/service7")
    public String service7() {

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    System.out.println("sleep interrupt ThreadId:"+Thread.currentThread().getId());
                }
                System.out.println("sleep over");
            }
        });
        map.put("t1",thread);
        map.put("t2",Thread.currentThread());
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            System.out.println("join interrupt ThreadId:"+Thread.currentThread().getId());
        }

        return "service7";
    }
}
